package com.yidu.BusinessParament.service.impl;

import com.yidu.BusinessParament.entity.Plate;
import com.yidu.BusinessParament.entity.Security;
import com.yidu.BusinessParament.mapper.SecurityMapper;
import com.yidu.BusinessParament.service.SecurityService;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 证券信息serviceimpl
 * @author :yimeng
 * @date :2022/12/5  16:26
 * @description:some description
 */
@Service
public class SecurityImpl implements SecurityService {
    @Resource
    private SecurityMapper securityMapper;

    //普通查询语句
    public List<Security> selectSecurity(Map map) {
        return securityMapper.selectSecurity(map);
    }

    /**
     * 查询总行数
     * @param security 证券信息实体类
     * @return 总行数
     */
    public int countSecurity(Security security) {
        HashMap map = new HashMap();
        map.put("strWhere",buildStrWhere(security));
        return securityMapper.countSecurity(map);
    }


    /**
     * 分页查询语句
     * @param page 页面
     * @param limit 分页
     * @param security  证券信息实体类
     * @return  分页查询的信息
     */
    public List<Security> limitSecurity(int page, int limit,Security security) {
        Map map = new HashMap();
        //页数
        map.put("page",(page-1)*limit);
        //显示行数
        map.put("limit",limit);
        //拼接条件
        map.put("strWhere",buildStrWhere(security));
        System.out.println(buildStrWhere(security)+000);
        return securityMapper.limitSecurity(map);
    }


    /**
     * 生成条件字符串
     * @param security 生成条件的参数
     * @return 条件字符串  and getRightText like '%%'
     */
    public String buildStrWhere(Security security){
        StringBuffer strWhere=new StringBuffer("");
        //证券编号
        if(security.getSecurityId()!=null && !security.getSecurityId().equals(""))
        {
            strWhere.append(" and securityId like '%" + security.getSecurityId() + "%'");
        }
        //交易所    && security.getExchangeName() !=null
        if(security.getExchangeName()!=0)
        {
            strWhere.append(" and exchangeName= " + security.getExchangeName());
        }
        //证券类型   && security.getSecurityType() !=null
        if(security.getSecurityType()!=0)
        {
            strWhere.append(" and securityType= " + security.getSecurityType());
        }
        System.out.println(strWhere+"进入分页查询");
        return  strWhere.toString();
    }

    //添加证券信息
    public int insertSecurity(Security security){
        return securityMapper.insertSecurity(security);
    }

    //修改证券信息
    public int updateSecurity(Security security) {
        return securityMapper.updateSecurity(security);
    }

    //根据证券编号进行单个删除
    public int deleteSecurity(String securityId) {
        return securityMapper.deleteSecurity(securityId);
    }

    //批量删除
    public int deleteMoreSecurity(String securityId) {
        //将id转为数据
        String[] split = securityId.split(",");
        //创建存放id的集合
        ArrayList<Object> equityDataIdList = new ArrayList<Object>();
        for (String id : split) {
            //将转换的id放入集合中
            equityDataIdList.add(id);
        }
        return securityMapper.deleteMoreSecurity(equityDataIdList);
    }

}
